﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace EBA.Framework
{
    /// <summary>
    /// 框架配置信息
    /// </summary>
    public class FrameworkConfiguration
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configPath"></param>
        public FrameworkConfiguration(string configPath = null)
        {
            this.ConfigPath = configPath;

            LoadConfig();
        }

        /// <summary>
        /// 配置文件的完整路径
        /// </summary>
        protected virtual string ConfigPath { get; set; }

        private void LoadConfig()
        {
            this.ExeConfiguration = AppRuntime.OpenConfiguration(this.ConfigPath);

            var eba = this.ExeConfiguration.GetSectionGroup("eba");

            if (eba == null)
            {
                eba = new ConfigurationSectionGroup();
                this.ExeConfiguration.SectionGroups.Add("eba", eba);
            }

            var framework = eba.Sections.Get("framework") as FrameworkConfigurationSection;

            if (framework == null)
            {
                framework = new FrameworkConfigurationSection();
                eba.Sections.Add("framework", framework);
            }

            this.Framework = framework;
        }


        /// <summary>
        /// 重新载入配置信息
        /// </summary>
        public void ReloadConfig()
        {
            this.LoadConfig();
        }

        /// <summary>
        /// 保存配置信息
        /// </summary>
        public void SaveConfig()
        {
            this.ExeConfiguration.Save();
        }

      
        /// <summary>
        /// 完整的插件配置文件
        /// </summary>
        protected Configuration ExeConfiguration { get; set; }

        /// <summary>
        /// 插件配置信息
        /// </summary>
        public FrameworkConfigurationSection Framework { get; set; }
   
    }
}
